﻿/**
 * Created with JetBrains PhpStorm.
 * User: skhero
 * Date: 14-4-10
 * Time: 下午4:28
 * To change this template use File | Settings | File Templates.
 */
define(function(require, exports, module) {
    return function(jQuery){
        (function($) {
            var httpconfig = {
                serverurl: "http://" + window.location.hostname + ":8705/Hantele.MPOS.PS.Server/QueryService2/",
                //javaserverurl: "http://" + window.location.hostname + ":8080/bp/",
                javaserverurl: "http://" + window.location.hostname + ":80/bp/",
                resrcurl: "http://" + window.location.hostname + ":8705/Hantele.MPOS.PS.Server/ResourceService2/",
                downloadurl:"http://"+window.location.hostname+":8888/downloadfile/"
            };
            /*seajs-content-start*/
            var getrandomstring = function() {
                var detail = function(len){
                    var len = len || 32;
                    var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; // 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
                    var maxPos = $chars.length;
                    var pwd = '';
                    for (var i = 0; i < len; i++) {
                        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
                    }
                    return pwd;
                }
                return {
                    getstr: detail
                };
            };
            var saveuserinfo = function(){
                var userinfo = {
                    UserID: "",
                    UserName: "",
                    SessionID: ""
                };
                return{
                    set: function(UserID, UserName, SessionID){
                        if($.cookie('mpos_uid') && $.cookie('mpos_name') && $.cookie('mpos_sid')){
                            userinfo.UserID = parseInt($.cookie('mpos_uid'));
                            userinfo.UserName = $.cookie('mpos_name');
                            userinfo.SessionID = parseInt($.cookie('mpos_sid'));
                        }
                        else if(SessionID){
                            userinfo.UserID = UserID;
                            userinfo.UserName = UserName;
                            userinfo.SessionID = SessionID;
                            $.cookie('mpos_uid', UserID);
                            $.cookie('mpos_name', UserName);
                            $.cookie('mpos_sid', SessionID);
                        }
                        else{
                            location.href = "#/login";
                        }
                    },
                    get: function(){
                        return userinfo;
                    }
                }
            };
            var httpservice = function($http){
                var transform = function(data){
                    return $.param(data);
                };
//                var serverurl = "./";

                return{
//                    post: function(controller, action, data){
//                        return $http.post(serveurl + controller + "/" + action, data, {
//                            headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
//                            transformRequest: transform
//                        });
//                    },
                    httpsget: function(url){
                        return $http.get(httpconfig.serverurl + url, '', {
                            headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                            transformRequest: transform
                        });
                    },
//                    getjson: function(filepath){
//                        return $http.get(serverurl + filepath, '', {
//                            headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
//                            transformRequest: transform
//                        });
//                    },
                    serverurl: httpconfig.serverurl,
                    javaserverurl:httpconfig.javaserverurl,
                    resrcurl:httpconfig.resrcurl,
                    downloadurl:httpconfig.downloadurl,
                    getJSON: function(url, getdata, cbk){
                        return $.getJSON(httpconfig.serverurl + url, getdata, function(data){
                            cbk(data);
                        })
                    },
                    //added by zhougansheng 
                    getResrcJSON: function (url, getdata, cbk) {
                        return $.getJSON(httpconfig.resrcurl + url, getdata, function (data) {
                            cbk(data);
                        })
                    },

                    post: function(url, postdata, cbk){
                        return $.post(httpconfig.serverurl + url, postdata, function(data){
                            cbk(data);
                        },"json")
                    },
                    postResrc: function(url, postdata, cbk){
                        return $.post(httpconfig.resrcurl + url, postdata, function(data){
                            cbk(data);
                        },"json")
                    },

                    upload: function (url, file) {
                        var formData = new FormData();
                        angular.forEach(file, function(file) {
                            formData.append('file[]', file);
                        });
                        return $http.post(httpconfig.serverurl + url, formData, {
                            headers: { 'Content-Type': undefined},
                            transformRequest: angular.identity
                        });
                    },
                    javaGetJson:function (url, getdata, cbk) {
                        return $.getJSON(httpconfig.javaserverurl + url, getdata, function (data) {
                            cbk(data);
                        });
                    },
                    javaPost:function (url, postdata, cbk) {
                        return $.post(httpconfig.javaserverurl + url, postdata, function (data) {
                            cbk(data);
                        });
                    }

                }
            };

            angular.module('mpos-service', [])
                .service('HttpService', ['$http', httpservice])
                .service('SaveUserInfo', saveuserinfo)
                .service('getRandomString', getrandomstring)
            /*seajs-content-end*/
        })(jQuery);
    }
})


